// pages/homepage/homepage.js
import {getListOpenId} from '../../Interface/taskInterface'
import {squadOPenidList} from '../../Interface/squadInterface'
import {formatTime} from '../../utils/utils'
import {getUserInfo} from '../../utils/authorization'
import {timings} from '../../utils/utils'
import {taskrecordGet} from '../../Interface/taskrecordIterface'
let time = 0
let taskLists = []
let taskNametaskid = null
let items = {}
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		taskList: [],
		modelShow: false,
		timing: null,
		time: "00:00:00",
		show: false,
		taskName: ""
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	clockOn(event,type){
		let item = event.currentTarget.dataset.item
		if(item.state===1){
			wx.showModal({
				title: "提示",
				content: "当前任务已结束",
			})
			return
		}
		if(this.data.show&&taskNametaskid!==null&&!type){
			if(item._id!==taskNametaskid){
				wx.showModal({
					title: "提示",
					content: "同时只能存在一个任务打卡",
					success(res){
					}
				})
				return 
			}
		}
		getUserInfo()
		.then(res=>{
			wx.navigateTo({
				url: `../clockon/clockon?id=${item._id}&name=${item.name}&squadid=${item.squadid}&labellist=${JSON.stringify(item.labellist)}`
			})
		}).catch(err=>{
			this.setData({
				modelShow: true
			})
		})
		
	},
	onconduct(){
		this.clockOn({
			currentTarget:{
				dataset:{
					item:items[0]
				}
			}
		},1)
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function () {
		let squadidlist = []
		await squadOPenidList()
			.then(res=>{
				squadidlist = res.data.map(item=>{
					return item._id
				})
			})
		await getListOpenId({squadid:squadidlist})
			.then(res=>{
				taskLists = JSON.parse(JSON.stringify(res.data))
				let taskList = res.data.map(item=>{
					item.y = formatTime(item.createTime,'Y年')
					item.m = formatTime(item.createTime,'M月')
					item.ym = `ddd${formatTime(item.createTime,'Y')}${formatTime(item.createTime,'M')}}`
					return item
				})
				let obj = {}
				taskList.forEach(item=>{
					obj[item.ym] = obj[item.ym]||[]
					obj[item.ym].push(item)
				})
				let ar = []
				for(let item in obj){
					ar.push(obj[item])
				}
				this.setData({
					taskList:ar
				})
			})
		await	taskrecordGet()
		.then(res=>{
			let show = res.data.length > 0
			this.setData({
				show
			})
			// 当前已经开始了
			if(show){
				let item = taskLists.filter(item=>{
					return item._id ===res.data[0].taskid
				}).map(item=>item)
				items = item
				taskNametaskid = item[0]._id
				this.setData({
					taskName: item[0].name
				})
				time = Math.floor((Date.now() - res.data[0].startTime)/1000)
				this.setTime()
			}
		})	
	},
	// 开始计时
	setTime(){
		if(this.data.timing!==null){
			clearInterval(this.data.timing)
		}
		this.data.timing = setInterval(()=>{
			time++
			this.setData({
				time: timings(time)
			})
		},1000)
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {
		clearInterval(this.data.timing)
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})